import type {
  CreateCrudOptionsProps,
  CreateCrudOptionsRet,
  ScopeContext,
} from '@fast-crud/fast-crud';

import { Tag } from 'ant-design-vue';

import { asyncFindUrlById } from '#/api';
import cities from '#/api/common/cities.json';
import { DictEnum, FileBizTypeEnum } from '#/enums/commonEnum';
import { $t } from '#/locales';
import {
  backendDict,
  createdTimeColumn,
  STATE_CONSTANT_DICT,
} from '#/plugins/fast-crud/common';
import { dateUtil } from '#/utils/dateUtil';

export function createCrudOptions(
  _: CreateCrudOptionsProps,
): CreateCrudOptionsRet {
  return {
    crudOptions: {
      columns: {
        logo: {
          title: 'logo',
          type: 'cropper-uploader',
          form: {
            component: {
              valueType: 'key',
              buildUrl: async (res: any) => {
                const file = await asyncFindUrlById(res);
                return file.data;
              },
              uploader: {
                param: {
                  bizType: FileBizTypeEnum.DEF_TENANT_LOGO,
                },
              },
            },
            col: { span: 24 },
          },
          viewForm: { ignoreUseCellComponent: true },
          column: { show: false, columnSetDisabled: true },
        },
        creditCode: {
          title: $t('devOperation.tenant.defTenant.creditCode'),
          type: 'text',
          column: { show: false },
        },
        name: {
          title: $t('devOperation.tenant.defTenant.name'),
          type: 'text',
          search: { show: true },
        },
        registerType: {
          title: $t('devOperation.tenant.defTenant.registerType'),
          type: 'dict-radio',
          dict: backendDict({
            type: 'DefTenantRegisterTypeEnum',
            isEnum: true,
          }),
          form: { show: false },
          viewForm: { show: true },
          column: { width: 80 },
        },
        abbreviation: {
          title: $t('devOperation.tenant.defTenant.abbreviation'),
          type: 'text',
          column: { show: false },
        },
        expirationTime: {
          title: $t('devOperation.tenant.defTenant.expirationTime'),
          type: 'datetime',
          form: {
            helper: {
              text: '有效期为空表示永久有效',
            },
            component: {
              format: 'YYYY-MM-DD HH:mm:ss',
              valueFormat: 'YYYY-MM-DD HH:mm:ss',
            },
          },
          valueBuilder({ value, row, key }) {
            if (value !== null) {
              row[key] = value;
            }
          },
          column: {
            width: 180,
            show: true,
            formatter: (context: ScopeContext) => {
              const record = context.row;
              if (record.expirationTime) {
                if (dateUtil(record.expirationTime).isBefore(Date.now())) {
                  return <Tag color="error">已过期</Tag>;
                } else if (
                  dateUtil(record.expirationTime).isBefore(
                    dateUtil().add(30, 'days'),
                  )
                ) {
                  const duration = dateUtil.duration(
                    dateUtil(record.expirationTime).diff(Date.now()),
                  );
                  if (duration.days() > 0) {
                    return (
                      <Tag color="warning">{duration.days() + 1}天后到期</Tag>
                    );
                  }
                  return (
                    <Tag color="warning">{duration.hours()}小时后到期</Tag>
                  );
                }
                return <Tag color="processing">{record.expirationTime}</Tag>;
              }

              return '永久有效';
            },
          },
        },
        state: {
          title: $t('devOperation.tenant.defTenant.state'),
          type: 'dict-radio',
          dict: STATE_CONSTANT_DICT,
          search: { show: true },
          addForm: { value: true },
          form: { component: { optionName: 'a-radio-button' } },
          column: { width: 80 },
          viewForm: {},
        },
        contactPerson: {
          title: $t('devOperation.tenant.defTenant.contactPerson'),
          type: 'text',
          column: { show: false },
        },
        contactPhone: {
          title: $t('devOperation.tenant.defTenant.contactPhone'),
          type: 'text',
          column: { show: false },
        },
        contactEmail: {
          title: $t('devOperation.tenant.defTenant.contactEmail'),
          type: 'text',
          column: { show: false },
        },
        provinceId: {
          title: '地区',
          type: 'text',
          column: { show: false },
          form: {
            component: {
              name: 'a-cascader',
              options: cities,
              checkStrategy: 'child',
            },
          },
        },
        address: {
          title: $t('devOperation.tenant.defTenant.address'),
          type: 'text',
          column: { show: false },
        },
        status: {
          title: $t('devOperation.tenant.defTenant.status'),
          type: 'dict-select',
          dict: backendDict({
            type: DictEnum.DefTenantStatusEnum,
          }),
          search: { show: true },
          form: { show: false },
          column: { show: true, width: 120 },
        },

        describe: {
          title: $t('devOperation.tenant.defTenant.describe'),
          type: 'textarea',
          column: { show: false },
        },
        ...createdTimeColumn({}),
      },
    },
  };
}

export const frontRules = {
  addForm: {
    rules: {},
  },
  editForm: {
    rules: {},
  },
};
